package com.songcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ObjectDao;
import com.pro.bean.Groups;
import com.songcy.dao.CardGroupDao;
import com.songcy.dao.FindGroupByNameDao;

public class AddToGroupsServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("UTF-8");
		String newGroupName = request.getParameter("newGroupName");
		if(newGroupName != null)
			newGroupName = new String(newGroupName.getBytes("ISO-8859-1"),"UTF-8");
		int groupid = Integer.parseInt(request.getParameter("groupid"));
		int cardid = Integer.parseInt(request.getParameter("cardid"));
		if(groupid != -1)
		{
			CardGroupDao cardGroupdao = new CardGroupDao();
			cardGroupdao.addCardGroup(cardid, groupid);
		}
		else
		{
			//set Groups
			Groups g = new Groups();
			g.setGroupname(newGroupName);
//			System.out.println(g.getGroupid());
//			System.out.println(g.getGroupname());
			ObjectDao objectdao = new ObjectDao();
			try {
				
				objectdao.add(g, ObjectDao.NOTINSERTID);
				
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			FindGroupByNameDao f = new FindGroupByNameDao();
			List l = f.findGroup(newGroupName);
			Iterator i = l.iterator();
			while(i.hasNext())
			{
				Groups group = (Groups)i.next();
				int gid = group.getGroupid();
				CardGroupDao cardGroupdao = new CardGroupDao();
				cardGroupdao.addCardGroup(cardid, gid);
			}
			
		}
		out.flush();
		out.close();
	}
}
